package cn.walls1717.mall.modules.pms.controller;


import cn.walls1717.mall.common.api.CommonPage;
import cn.walls1717.mall.common.api.CommonResult;
import cn.walls1717.mall.common.util.ComResultUtil;
import cn.walls1717.mall.modules.pms.dto.PageListDTO;
import cn.walls1717.mall.modules.pms.dto.ProductAttributeCateDTO;
import cn.walls1717.mall.modules.pms.model.PmsProductAttributeCategory;
import cn.walls1717.mall.modules.pms.service.PmsProductAttributeCategoryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 产品属性分类表 前端控制器
 * </p>
 *
 * @author walls1717
 * @since 2022-04-09
 */
@RestController
@RequestMapping("/productAttributes/categories")
public class PmsProductAttributeCategoryController {

    private final PmsProductAttributeCategoryService attributeCategoryService;
    @Autowired
    public PmsProductAttributeCategoryController(PmsProductAttributeCategoryService attributeCategoryService) {
        this.attributeCategoryService = attributeCategoryService;
    }

    @ApiOperation("商品属性类型分页展示")
    @GetMapping()
    public CommonResult<CommonPage<PmsProductAttributeCategory>> pageList(@Valid PageListDTO pageListDTO) {
        Page<PmsProductAttributeCategory> list = attributeCategoryService.pageList(pageListDTO);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("商品属性类型添加")
    @PostMapping()
    public CommonResult<?> createAttributeCategory(PmsProductAttributeCategory attributeCategory) {
        boolean result = attributeCategoryService.createAttributeCategory(attributeCategory);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("商品属性类型编辑")
    @PutMapping("{id}")
    public CommonResult<?> updateAttributeCategory(PmsProductAttributeCategory attributeCategory) {
        boolean result = attributeCategoryService.updateById(attributeCategory);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("商品属性类型删除")
    @DeleteMapping("{id}")
    public CommonResult<?> deleteAttributeCategory(@PathVariable Long id) {
        boolean result = attributeCategoryService.removeById(id);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("查询商品属性类型的规格以及其参数，用于特定页面中筛选属性一栏的级联菜单展示")
    @GetMapping("attributes")
    public CommonResult<List<ProductAttributeCateDTO>> listWithAttr() {
        List<ProductAttributeCateDTO> list = attributeCategoryService.listWithAttr();
        return CommonResult.success(list);
    }


}

